package monster.comm.action;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import monster.comm.log.ILog;
import monster.comm.log.LogService;
import monster.comm.util.SysConstant;

/**
 * ajax分页action基类
 * @author wanghong
 * 2013-1-4
 * BaseJsonListAction.java
 *
 */
public abstract class BaseJsonListAction<T,U> extends BaseJsonAction {
	private static final long serialVersionUID = 1L;
	private static ILog log = LogService.getLog(BaseJsonListAction.class);
	public PageVO pageVO;

    public BaseJsonListAction(){
	     
    }
	public PageVO getPageVO() {
		return pageVO;
	}

	public void setPageVO(PageVO pageVO) {
		this.pageVO = pageVO;
	}
	/**
	 * 设置分页类型
	 * @param type
	 */
	private void setPageBarType(String type){
		pageVO.setPageBarType(type);
	}
	/**
	 * 执行分页,利用反射调用
	 * @param service
	 */
    public List<U> doPage(T service,U vo,Map<String,Object> map){
    	setPageBarType(SysConstant.page_split_ajax_bar_jquery);
    	Class<?> c = service.getClass();
    	Method method=null;
		try {
			method = c.getDeclaredMethod("getRowCount", vo.getClass());
			Object [] args =  new Object[1];
	    	args[0] = vo;
	    	int r = Integer.parseInt(method.invoke(service, args).toString());
	    	this.pageVO.setRowCount(r);
	    	this.doPageBar();
	    	method = c.getDeclaredMethod("selectPage", Map.class);
	    	args =  new Object[1];
	    	args[0] = map;
			map.put("offset", String.valueOf((pageVO.curPage-1)*pageVO.pageSize+1));
			map.put("limit", String.valueOf(pageVO.curPage*pageVO.pageSize));
	    	@SuppressWarnings("unchecked")
			List<U> list = (List<U>)method.invoke(service, args);
	    	return list;
		} catch (Exception e) {
		   log.error(e.getMessage(), e);
		}
		return new ArrayList<U>();
    }
	public void doPageBar() {
		getPageBar();

	}

   /**
    * 获得分页条
    */
	private void getPageBar() {
		try {
			int pageSize = pageVO.pageSize;
			if (pageSize == 0)
				pageSize = pageVO.default_pagesize;
			if (pageSize == 0)
				pageSize = 1;
			pageVO.pageSize = pageSize;

			pageVO.pageCount = pageVO.rowCount / pageVO.pageSize
					+ (pageVO.rowCount % pageVO.pageSize > 0 ? 1 : 0);// 获得页数
			log.info("pageCount=" + pageVO.pageCount);

			if (pageVO.pageCount < 1) {
				pageVO.curPage = 0;
				pageVO.action = "";
			}
			String action_str = pageVO.action;
			if (action_str == null || action_str.trim().length() == 0) {
				pageVO.action = "";
			} else {
				pageVO.action = action_str;
			}

			int curPage = pageVO.curPage;// 当前第几页
			if (curPage == 0) {
				pageVO.curPage = 1;
			} else {
				pageVO.curPage = curPage;
			}

			if (pageVO.action.equals("")
					|| pageVO.action.equals("first"))// 初始化
			{
				if (pageVO.rowCount > 0) {
					pageVO.curPage = 1;
					log.info("first curPage="+ pageVO.curPage);
				}
			} else if (pageVO.action.equals("next")) {
				if (pageVO.rowCount > 0) {
					pageVO.curPage = pageVO.curPage + 1;
					if (pageVO.curPage > pageVO.pageCount)
						pageVO.curPage = pageVO.pageCount;
					log.info("next curPage=" + pageVO.curPage);
				}
			} else if (pageVO.action.equals("preve")) {
				if (pageVO.rowCount > 0) {
					pageVO.curPage = pageVO.curPage - 1;
					if (pageVO.curPage < 1)
						pageVO.curPage = 1;
					log.info("preve curPage="+ pageVO.curPage);
				}
			} else if (pageVO.action.equals("last")) {
				pageVO.curPage = pageVO.pageCount;
				log.info("last curPage=" + pageVO.curPage);
			}
			if (pageVO.curPage > pageVO.pageCount)
				pageVO.curPage = pageVO.pageCount;

			if (pageVO.curPage == 0)
				pageVO.curPage = 1;

            if (SysConstant.page_split_bar_jquery
					.equals(pageVO.pageBarType)) {
				setJqueryPageBar(pageVO);
			} else {
				setJqueryAjaxPageBar(pageVO);
			}
		} catch (Exception e) {
			log.error(e.getMessage(),e);
		}
	}
	/**
	 * 设置jquery工具条
	 */
	private void setJqueryPageBar(PageVO pageVO) {
		StringBuilder sb = new StringBuilder(300);		
		sb.append("<script>\n");
		sb.append("  var paging;");
		sb.append("  jQuery(function(){\n");
		sb.append("    paging=jQuery(\"#paging\").myPagination(\n");
		sb.append("      {\n");
		sb.append("        rowCount:").append(pageVO.rowCount).append(",\n");
		sb.append("        currPage:").append(pageVO.curPage).append(",\n");
		sb.append("        pageCount:").append(pageVO.pageCount).append(",\n");
		sb.append("        ajax:{onClick:pageselectCallback},\n");
		sb.append("        panel:{tipInfo_on:true,links:'javascript:void(0);',tipInfo_css:{width:'22px',height:'18px'}}\n");
		sb.append("     });\n");
		sb.append("  });\n");
		sb.append(" function pageselectCallback(page){\n");
		sb.append(" jQuery(\"form\").find(\"input[name='pageVO\\\\.curPage']\").val(page);\n");
		sb.append(" jQuery(\"form\").submit(); \n");
		sb.append(" return false;//返回true连接生效,false则连接失效 \n");
		sb.append(" }\n");
		sb.append(" jQuery(function(){");
		sb.append("  jQuery(\"form\").append('<input  name=\"pageVO.curPage\" type=\"hidden\" value=\"").append(pageVO.curPage).append("\"/>')\n");
		sb.append("  jQuery(\"form\").append('<input  name=\"pageVO.action\" type=\"hidden\" value=\"jump\"/>')\n");
		sb.append("  });\n");
		sb.append(" jQuery(function(){");
		sb.append("  jQuery(\"form\").find('input[type=\"text\"]').blur(function(){\n");
		sb.append("  if(!/^[0-9]*$/.test(jQuery(this).val())){alert(\"请输入正确的数字!\");return;}");
		sb.append("  jQuery(\"form\").find(\"input[name='pageVO\\\\.curPage']\").val(jQuery(this).val());\n");
		sb.append("  jQuery(\"form\").submit(); \n");
		sb.append("  })\n");
		sb.append("  });\n");
		sb.append("</script>");
		pageVO.pageBar = sb.toString();
	}

	/**
	 * 设置jquery ajax工具条
	 */
	private void setJqueryAjaxPageBar(PageVO pageVO) {
		StringBuilder sb = new StringBuilder(300);		
		sb.append("<script>\n");
		sb.append("  var paging;");
		sb.append("  jQuery(function(){\n");
		sb.append("    paging=jQuery(\"#paging\").myPagination(\n");
		sb.append("      {\n");
		sb.append("        rowCount:").append(pageVO.rowCount).append(",\n");
		sb.append("        currPage:").append(pageVO.curPage).append(",\n");
		sb.append("        pageCount:").append(pageVO.pageCount).append(",\n");
		sb.append("        cssStyle:'yahoo2',ajax:{onClick:pageselectCallback},\n");
		sb.append("        panel:{tipInfo_on:true,links:'javascript:void(0);',tipInfo_css:{width:'22px',height:'18px'}}\n");
		sb.append("     });\n");
		sb.append("  });\n");
		sb.append(" function pageselectCallback(page){\n");
		sb.append(" initPageListData(page)\n");
		sb.append(" }\n");
		sb.append(" jQuery(function(){");
		sb.append("  \n");
		sb.append("  jQuery(\"#paging\").append('<input  name=\"action\" type=\"hidden\" value=\"jump\"/>')\n");
		sb.append("  });\n");
		sb.append(" jQuery(function(){");
		sb.append("  jQuery('#paging input[type=\"text\"]').blur(function(){\n");
		sb.append("  if(!/^[0-9]*$/.test(jQuery(this).val())){art.dialog.alert(\"请输入数字\",function(){},\"error\");return;}");
		sb.append("  initPageListData(jQuery(this).val())  \n");
		sb.append("  })\n");
		sb.append("  });\n");
		sb.append("</script>");
		pageVO.pageBar = sb.toString();

	}

}
